package com.fu.servlet;

import com.fu.bean.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @author 付大大
 * @description
 * @create 2021-01-24 19:54
 */
@WebServlet("/addStudentServlet")
public class AddStudentServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String age = req.getParameter("age");
        String score = req.getParameter("score");

        Student student = new Student();
        student.setUsername(username);
        student.setAge(Integer.parseInt((age)));
        student.setScore(Integer.parseInt(score));

        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("e:\\stu.txt", true));
        bufferedWriter.write(student.getUsername() + "," + student.getAge() + "," + student.getScore());
        bufferedWriter.newLine();
        bufferedWriter.close();

        resp.getWriter().write("添加成功。两秒后自动跳转到首页");
        resp.setHeader("Refresh","2;URL=/stu/index.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
